package com.huangyi;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
       //水果成篮
        class Solution {
            public int totalFruit(int[] f) {
                int n = f.length,ret = 0;
                int[] hash = new int[n+1];
                for(int left = 0,right =0,kind = 0;right<n;right++){
                    if(hash[f[right]] == 0){
                        kind++;
                    }
                    hash[f[right]]++;
                    while(kind>2){
                        hash[f[left]]--;
                        if(hash[f[left]] == 0){
                            kind--;
                        }
                        left++;
                    }
                    ret = Math.max(ret,right-left+1);
                }
                return ret;
            }
        }



        //找到字符串中所有字母异位词
        class Solution2 {
            public List<Integer> findAnagrams(String ss, String pp) {
                char[] s = ss.toCharArray();
                char[] p = pp.toCharArray();
                int[] hash1 = new int[26];
                for(char ch : p){hash1[ch - 'a']++;}
                int[] hash2 = new int[26];
                List<Integer> ret = new ArrayList<>();
                for(int left = 0,right = 0,count =0;right<s.length;right++){
                    if(++hash2[s[right]-'a'] <= hash1[s[right]-'a']){count++;}
                    while(right-left+1 > p.length){
                        if(hash2[s[left]-'a']-- <= hash1[s[left]-'a']){count--;}
                        left++;
                    }
                    if(count == p.length){
                        ret.add(left);
                    }

                }
                return ret;
            }
        }
    }
}